/*
 * $Id: PendingChangelistsDragAdapter.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.ui.dnd;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.IChangesResult;
import net.sourceforge.p4eclipse.core.api.IInfoResult;
import net.sourceforge.p4eclipse.core.api.IOpenedResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PendingChangelistsDragAdapter extends DragSourceAdapter
{
	private ISelectionProvider selectionProvider; 
	
	/**	 Creates a new PendingChangelistsDragAdapter instance. */
	public PendingChangelistsDragAdapter(ISelectionProvider provider) 
	{
		selectionProvider = provider;
	}	

	/** @see DragSourceAdapter#dragSetData(org.eclipse.swt.dnd.DragSourceEvent) */
	public void dragSetData(DragSourceEvent event) 
	{	
		if (OpenedResultTransfer.getInstance().isSupportedType(event.dataType))
		{
			event.data = getSelectedOpenedResult();
			return;
		}
	}
		
	/** @see DragSourceListener#dragStart(DragSourceEvent) */
	public void dragStart(DragSourceEvent event)
	{
		IInfoResult infoResult = PerforceUIPlugin.getClient().info();
		
		IStructuredSelection selection = (IStructuredSelection)selectionProvider.getSelection();
		for (Iterator it = selection.iterator(); it.hasNext(); ) 
		{
			Object next = it.next();
			if (!(next instanceof IOpenedResult))
			{
				event.doit = false;
				return;
			}
			else
			{
				IOpenedResult openedResult = (IOpenedResult)next;
				if (!(infoResult.getClientName().equals(openedResult.getClientName())))
				{
					event.doit = false;
					return;
				}								
			}
		}
				
		event.doit = true;
	}
	
	private IOpenedResult[] getSelectedOpenedResult() 
	{
		ISelection selection = selectionProvider.getSelection();
		if (!(selection instanceof IStructuredSelection) || selection.isEmpty()) 
			return null;

		IStructuredSelection structuredSelection = (IStructuredSelection)selection;
		if (structuredSelection == null)
			return null;

		List result = new ArrayList();
		for (Iterator it = structuredSelection.iterator(); it.hasNext(); )
		{
			Object next = it.next();
			if (next instanceof IOpenedResult)
				result.add(next);
		}
		
		return (IOpenedResult[])result.toArray(new IOpenedResult[result.size()]);
	}
}
